﻿<?php
	$link = mysql_connect('localhost','root');
	$p = mysql_select_db('asd',$link);
	mysql_set_charset('utf8',$link);
	header("Content-Type:text/html;charset=UTF-8");
	
	  
		$url1="http://lieche.58.com/checi/ktpukuai.html";
		$url2="http://lieche.58.com/checi/puke.html";
		$url3="http://lieche.58.com/checi/ktkuai.html";
		$url4="http://lieche.58.com/checi/kuai.html";
		$url5="http://lieche.58.com/checi/pukuai.html";
		$url6="http://lieche.58.com/checi/zdtekuai.html";
		$url7="http://lieche.58.com/checi/dongche.html";
		$url8="http://lieche.58.com/checi/ktpuke.html";
		$url9="http://lieche.58.com/checi/chengji.html";
		$url10="http://lieche.58.com/checi/kttekuai.html";
		$url11="http://lieche.58.com/checi/gaotie.html";
		$url12="http://lieche.58.com/checi/linke.html";	
		
		$r1=file_get_contents($url1);		
		$r2=file_get_contents($url2);
		$r3=file_get_contents($url3);	
		$r4=file_get_contents($url4);	
		$r5=file_get_contents($url5);	
		$r6=file_get_contents($url6);	
		$r7=file_get_contents($url7);	
		$r8=file_get_contents($url8);
		$r9=file_get_contents($url9);
		$r10=file_get_contents($url10);
		$r11=file_get_contents($url11);
		$r12=file_get_contents($url12);	
		
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r1,$ktpukuai))
		{
			//print_r($ktpukuai);	
                     //253			
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r2,$puke))
		{
			print_r($puke);	//340	
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r3,$ktkuai))
		{
			//print_r($ktkuai);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r4,$kuai))
		{
			//print_r($kuai);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r5,$pukuai))
		{
			//print_r($pukuai);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r6,$zdtekuai))
		{
			//print_r($zdtekuai);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r7,$dongche))
		{
			//print_r($dongche);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r8,$ktpuke))
		{
			//print_r($ktpuke);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r9,$chengji))
		{
			//print_r($chengji);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r10,$kttekuai))
		{
			//print_r($kttekuai);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r11,$gaotie))
		{
			//print_r($gaotie);		
		}
		if(preg_match_all("/<li><a href=\'(.*?)\' target=\"_blank\">.*?<\/a><\/li>/si",$r12,$linke))
		{
			//print_r($linke);		
			//46
		}
		
		
		$j=0;
while($j<253)
{		
		$url = "http://lieche.58.com".$ktpukuai[1][$j]."/";
		$r = file_get_contents($url);
		
			//总表
			echo'<br/>';
			echo'<br/>';
			if(preg_match("/<span class=\"red\">(.*?)<\/span><\/td><td>(.*?)<\/td>/si",$r,$name))	
			{
			//print_r($name);
			echo'<br/>';
			echo '车号:'.$name[1].'<br/>类型:'.$name[2];
			}	   

			if(preg_match("/<td><a href=\".*?\" target=\"_blank\">(.*?)<\/a><\/td>/si",$r,$start))		
			{	
			//print_r($start);
			echo'<br/>';
			echo 'start:'.$start[1];			
			}
			
			if(preg_match_all("/<td><a href=\".*?\" target=\"_blank\">(.*?)<\/a><\/td>/si",$r,$end))		
			{	
			//print_r($end);
			echo'<br/>';
			echo 'end:'.$end[1][1];			
			}	
			
			if(preg_match_all("/<td><span class=\"red\">(.*?)<\/span><\/td>/si",$r,$time))
			{
			echo '<br/>';
			echo 'stime:'.$time[0][1];	
			echo '<br/>';
			echo 'etime:'.$time[0][2];	
			//print_r($time);				
			}	
			
			if(preg_match("/<td>([0-9]?[0-9])小时([0-9]?[0-9])分钟<\/td><td>(.*?)<\/td>/si",$r,$alltime))
			{									 
			echo '<br/>';
			//print_r($alltime); 
			echo 'alltime:'.$alltime[1].'小时'.$alltime[2].'分钟';	
			echo '<br/>';
			echo '全长：'.$alltime[3];
			}
			
			echo '<br/><br/><br/>';			
		
	
		if(preg_match("/共经过：<b class=\"red\">(.*?)<\/b>站<br \/>/si",$r,$zhanci))
		{		
		echo '&nbsp&nbsp&nbsp';
		}


		$i=0;  
		
			//途中站
		
			if(preg_match_all("/<td style=\"text-align:left;padding-left:45px;\"><a href=\".*?\"target=\"_blank\">(.*?)<\/a><\/td>/si",$r,$fenzhan))
			{		
			//print_r($fenzhan);		  
			}			
            if(preg_match_all("/<td style=\"text-align:left;padding-left:20px;\">(.*?)<\/td>/si",$r,$zday))
			{		 
			//print_r($zday);		
			}
			if(preg_match_all("/<td style=\"text-align:left;padding-left:20px;\">(.*?)<\/td>/si",$r,$zalltime))
			{		
			//print_r($zalltime);		
			}			
			if(preg_match_all("/<td>(.*?)<\/td>/si",$r,$ztime))
			{	
			//print_r($ztime);			
			}			
			if(preg_match_all("/<td style=\"text-align:left;padding-left:25px;\">(.*?)<\/td>/si",$r,$zprice1))
			{				
			//print_r($zprice1);			
			}			
			if(preg_match_all("/<td style=\"text-align:left;padding-left:15px;\">(.*?)<\/td>/si",$r,$zprice2))
			{				
			//print_r($zprice2);			
			}
			
		while($i<$zhanci[1])
		{
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<br/>';
			echo @$fenzhan[1][$i];		
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $zday[1][2*$i];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $zalltime[0][2*$i+1];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp ';
			echo $ztime[0][14+$i*7];			
			//echo strtotime("$ztime[0][14+$i*7]':00'");			
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $ztime[0][15+$i*7];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $ztime[0][16+$i*7];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $ztime[0][17+$i*7];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $ztime[0][18+$i*7];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $zprice1[0][$i];
			echo '&nbsp&nbsp&nbsp&nbsp&nbsp';
			echo $zprice2[0][$i];
			
		$i=$i+1;
		
		}				
						
$j=$j+1;

 $sql="insert into type (type_tra_num,type_tra) VALUES ('$name[1]','$name[2]');"; 
    $result=mysql_query($sql);
	echo '<br/>';
    if($result) 	  
			echo "添加到数据表中<br>";
    else 	echo "没有添加进数据表，请重新输入.<br>";	
	
}		

		
							
							

		
		
	
		

		
?>